package com.osdp.uc.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.osdp.common.util.ConvertUtil;
import com.osdp.uc.entity.UcOrgType;
import com.osdp.uc.service.IUcOrgTypeService;
import com.osdp.uc.vo.UcOrgTypeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("/uc/orgType")
@Api("组织体系管理")
public class UcOrgTypeController {
    @Autowired
    IUcOrgTypeService orgTypeService;

    @RequestMapping(value = "/pageList", method = RequestMethod.GET)
    @ApiOperation(value = "组织机构类型列表", tags = "组织机构类型列表")
    public IPage<UcOrgTypeVo> queryPageList(UcOrgTypeVo orgTypeVo,
                                        @RequestParam(name = "pageNo", defaultValue = "1") int pageNo,
                                        @RequestParam(name = "pageSize", defaultValue = "10") int pageSize){
        return orgTypeService.pageList(orgTypeVo, new Page<>(pageNo,pageSize));
    }

    @RequestMapping(value = "/allList", method = RequestMethod.GET)
    @ApiOperation(value = "查询组织机构类型列表", tags = "查询组织机构类型列表")
    public List<UcOrgTypeVo> queryList(UcOrgTypeVo ucOrgTypeVo){
        return orgTypeService.queryList(ucOrgTypeVo);
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ApiOperation("新增类型")
    public boolean insert(@RequestBody UcOrgTypeVo orgTypeVo) {
        UcOrgType orgType = ConvertUtil.sourceToTarget(orgTypeVo, UcOrgType.class);
        return orgTypeService.save(orgType);
    }

    @PutMapping("/update")
    @ApiOperation("修改类型")
    public boolean update(@RequestBody  UcOrgTypeVo orgTypeVo) {
        UcOrgType orgType = ConvertUtil.sourceToTarget(orgTypeVo, UcOrgType.class);
        return orgTypeService.updateById(orgType);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除类型")
    public boolean delete(@RequestParam(name = "id", required = true) String id) {
        return orgTypeService.removeById(id);
    }

    @RequestMapping(value = "/getById", method = RequestMethod.GET)
    @ApiOperation("根据ID获取对象")
    public UcOrgTypeVo getById(@RequestParam(name = "id", required = true) String id){
        UcOrgType orgType = orgTypeService.getById(id);
        return ConvertUtil.sourceToTarget(orgType,UcOrgTypeVo.class);
    }

}
